package examination.Qunar_09_21;


public class Solution2 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 将一段包含文本编辑命令的字符串input转换为最终的编辑结果
     * @param input string字符串 
     * @return string字符串
     */
    public String transform (String input) {
        String[] strings = input.split("\\\\");
        StringBuilder sb = new StringBuilder();
        int len = strings.length;
        for (int i = 0; i < len - 1; i++) {
            if (strings[i].length() != 0) {
                sb.append(strings[i]);
                if (i != len - 2) {
                    sb.append('\\');
                }
            }
        }
        String res = "";
        if (strings[len - 1].startsWith("delete{")) {
            String end = strings[len - 1];
            long n = Long.parseLong(end.substring(7, end.length() - 1));
            if (sb.length() > n) {
                res = sb.substring(0, sb.length() - (int) n);
            }
        } else {
            return sb.toString();
        }
        return res;
    }

    public static void main(String[] args) {
        String str = new String("abc\\\\dab\\delete{5653647364624562}");
        Solution2 solution2 = new Solution2();
        System.out.println(solution2.transform(str));
//        String[] strings = str.split("\\\\");
//        for (String string : strings) {
//            System.out.println(string);
//        }
//        System.out.println(strings[strings.length - 1].substring(7, strings[strings.length - 1].length() - 1));
    }
}